package com.qqtech.qquan.homemaking.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.homemaking.model.HomemakingBase;
import com.qqtech.qquan.homemaking.model.HomemakingOrder;
import com.qqtech.qquan.homemaking.model.HomemakingOrderQuery;
import com.qqtech.qquan.homemaking.model.HomemakingOrderVo;
import com.qqtech.qquan.homemaking.service.HomemakingBaseService;
import com.qqtech.qquan.homemaking.service.HomemakingOrderService;
import com.qqtech.qquan.homemaking.service.HomemakingTimeService;
import com.qqtech.qquan.order.enums.OrderBaseOrderTypeDictEnum;
import com.qqtech.qquan.order.enums.OrderDeliveryTypeDictEnum;
import com.qqtech.qquan.order.model.OrderBase;
import com.qqtech.qquan.order.model.OrderBaseQuery;
import com.qqtech.qquan.order.model.OrderDelivery;
import com.qqtech.qquan.order.model.OrderItem;
import com.qqtech.qquan.order.service.OrderBaseService;
import com.qqtech.qquan.order.service.OrderDeliveryService;
import com.qqtech.qquan.order.service.OrderItemService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.user.model.UserBase;
import com.qqtech.qquan.user.service.UserBaseService;

@Controller
@RequestMapping(value = "/homemaking/order")
public class HomemakingOrderController extends BaseAdminController {
	@Resource
	private HomemakingOrderService homemakingOrderService;

	@Resource
	private HomemakingTimeService homemakingTimeService;

	@Resource
	private HomemakingBaseService homemakingBaseService;
	
	@Resource
	private OrderBaseService orderBaseService;

	@Resource
	private OrderDeliveryService orderDeliveryService;

	@Resource
	private OrderItemService orderItemService;

	@Resource
	private UserBaseService userBaseService;

	@Override
	protected BaseService<HomemakingOrder> getBaseService() {
		return homemakingOrderService;
	}

	@RequestMapping(value = "/list/{baseId}", method = RequestMethod.GET)
	public String list(@PathVariable Integer baseId, Model model) {
		model.addAttribute("baseId", baseId);
		return "/homemaking/homemakingorder-list";
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(HomemakingOrderQuery query,
			HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		List<HomemakingOrderVo> list = null;
		int total = 0;
		if (query != null && query.getBaseId() != null) {
			String rows = (String) request.getParameter("rows"); // 每页条数
			query.setSize(Integer.valueOf(rows));
			HomemakingBase homemakingBase = homemakingBaseService
					.getById(query.getBaseId());
			if (homemakingBase != null) {
				Pageable pageKit = new PageKit(query.getPage(),query.getSize(),
						new PageOrder(Direction.ASC, "`status`"),new PageOrder(Direction.ASC, "serviceAt"));
				Page<HomemakingOrderVo> page = homemakingOrderService
						.queryPageList(query, pageKit);
				list = page.getContent();
				total = page.getNumberOfElements();
			}
		}
		adminPage.setRows(list);
		adminPage.setTotal(total);
		return adminPage;
	}
	
	@RequestMapping(value = "/detail")
	public String detail(OrderBaseQuery orderBaseQuery, Model model) {
		OrderBase order = null;
		if (orderBaseQuery.getId() != null) {
			order = orderBaseService.getById(orderBaseQuery.getId());
		} else {
			order = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
		}
		if (order != null) {
			List<OrderItem> itemList = orderItemService.findByOrderNo(order.getOrderNo());
			OrderDelivery delivery = orderDeliveryService.getByOrderNoAndType(order.getOrderNo(),
					OrderDeliveryTypeDictEnum.SELLERSERVICE.getCode());
			UserBase seller = userBaseService.getById(order.getSellerId());
			if (order.getOrderType().intValue() == OrderBaseOrderTypeDictEnum.HOMEMAKING.getCode()) {
				model.addAttribute("homemakingOrder", homemakingOrderService.getByOrderNo(order.getOrderNo()));
			}
			model.addAttribute("order", order);
			model.addAttribute("itemList", itemList);
			model.addAttribute("delivery", delivery);
			model.addAttribute("sellerName", seller.getNick());
		}
		return "/homemaking/homemakingorder-detail";
	}
	
}
